package com.uxtc.cloud.service.impl;

import com.uxtc.cloud.constant.MessageConstant;
import com.uxtc.cloud.domain.SecurityUser;
import com.uxtc.cloud.common.entity.UserDto;
import com.uxtc.cloud.mapper.UserServiceDao;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户管理业务类
 *
 * @author 鱼仔
 * @date 2020/6/19
 */
@Component
@AllArgsConstructor
@Slf4j
public class UserServiceImpl implements UserDetailsService {

    @Resource
    private HttpServletRequest request;

    @Resource
    UserServiceDao userServiceDao;

    /**
     * 获取用户数据基本数据和权限数据
     *
     * @param username 用户名
     * @return
     */
    @Override
    public UserDetails loadUserByUsername(String username) {
        String clientId = request.getParameter("client_id");
        UserDto userDto = userServiceDao.loadUserByUsername(username);
        if (userDto == null) {
            throw new UsernameNotFoundException(MessageConstant.USERNAME_PASSWORD_ERROR);
        }
        if (userDto.getStatus() == 1) {
            throw new DisabledException(MessageConstant.ACCOUNT_LOCKED);
        }
        List<String> roles = userServiceDao.getRoles(userDto.getId());
        userDto.setRoles(roles);
        userDto.setClientId(clientId);
        SecurityUser securityUser = new SecurityUser(userDto);
        return securityUser;
    }
}
